package com.company.calendar.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.company.calendar.entity.CustomDate;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * 自定义日期Mapper接口
 * 
 * @author System
 * @since 1.0.0
 */
@Mapper
public interface CustomDateMapper extends BaseMapper<CustomDate> {
    
    /**
     * 根据公司ID和日期查找自定义日期
     */
    @Select("SELECT * FROM custom_dates WHERE company_id = #{companyId} AND custom_date = #{customDate} AND is_deleted = 0")
    CustomDate findByCompanyIdAndCustomDateAndIsDeletedFalse(@Param("companyId") Long companyId, 
                                                            @Param("customDate") LocalDate customDate);
    
    /**
     * 根据公司ID查找指定日期范围的自定义日期
     */
    @Select("SELECT * FROM custom_dates WHERE company_id = #{companyId} " +
            "AND custom_date BETWEEN #{startDate} AND #{endDate} " +
            "AND is_deleted = 0 ORDER BY custom_date")
    List<CustomDate> findByCompanyIdAndDateRange(@Param("companyId") Long companyId,
                                                 @Param("startDate") LocalDate startDate,
                                                 @Param("endDate") LocalDate endDate);
    
    /**
     * 根据公司ID查找所有自定义日期
     */
    @Select("SELECT * FROM custom_dates WHERE company_id = #{companyId} AND is_deleted = 0 ORDER BY custom_date")
    List<CustomDate> findByCompanyIdAndIsDeletedFalseOrderByCustomDate(@Param("companyId") Long companyId);
    
    /**
     * 根据公司ID和年份查找自定义日期
     */
    @Select("SELECT * FROM custom_dates WHERE company_id = #{companyId} " +
            "AND YEAR(custom_date) = #{year} " +
            "AND is_deleted = 0 ORDER BY custom_date")
    List<CustomDate> findByCompanyIdAndYear(@Param("companyId") Long companyId, @Param("year") Integer year);
    
    /**
     * 检查指定日期是否存在自定义日期
     */
    @Select("SELECT COUNT(*) FROM custom_dates WHERE company_id = #{companyId} AND custom_date = #{customDate} AND is_deleted = 0")
    int existsByCompanyIdAndCustomDateAndIsDeletedFalse(@Param("companyId") Long companyId, 
                                                       @Param("customDate") LocalDate customDate);
}
